const Router = require('koa-router');
const router = new Router();
const {db, Op,QueryTypes } = require("../../models/db");

// 自动离店设置
router.post('/sets/help', async ctx => {
    let routeName = ctx.request.body.routeName
    let model = await db.models['help'].findOne({
        where: {
            routeName
        }
    })
    if(!model) return ctx.err('还没有设置帮助文档')

    return ctx.suc('', model.cont)
})
router.post('/sets/getHelpById', async ctx => {
    let routeName = ctx.request.body.routeName
    let model = await db.models['help'].findOne({
        where: {
            routeName
        }
    })
    if(!model) {
        return ctx.suc('', '<h2 style="text-align: center;">还没有此页面的 帮助文档</h2>')
    }

    return ctx.suc('', model.cont)
})
router.post('/sets/saveHelp', async ctx => {
    let routeName = ctx.request.body.routeName
    let cont = ctx.request.body.cont
    let model = await db.models['help'].findOne({
        where: {
            routeName
        }
    })
    if(model) {
        model.cont = cont
        await model.save()
    }
    else {
        await db.models['help'].create({
            routeName,
            cont
        })
    }
    return ctx.suc('保存成功')
})

module.exports = router;